loadfile("lib/turtle_status.lua")()
loadfile("lib/movement.lua")()
loadfile("lib/logf.lua")()


function harvest()
	activateDrain()

	pickUpSeeds()
	
	plantSeeds()
end

function activateDrain()
	logf("INFO", "harvest", "Activating the drain")
	logRednet("Activating the drain")
	redstone.setOutput("left", true)
	logf("INFO", "harvest", "Waiting for crops to be picked up")
	logRednet("Waiting for crops to be picked up")
	os.sleep(60)
	logf("INFO", "harvest", "Stoping the drain")
	logRednet("Stoping the drain")
	redstone.setOutput("left", false)
end

--	up(3)
--	turtle.turnLeft()
--	turtle.turnLeft()
--	forward(24)
--	turtle.turnRight()
--	forward(11)

--	down(1)
--	turtle.turnLeft()
--	forward(2)
--	down(3)
--	turtle.turnLeft()
--	turtle.turnLeft()
--end

function pickUpSeeds()
	turtle.turnRight()
	up(1)
	turtle.select(2)
	turtle.suck()
	up(2)
	turtle.select(3)
	turtle.suck()
	turtle.select(4)
	turtle.suck()
end

function plantSeeds()
	turtle.turnRight()
	turtle.turnRight()
	forward(16)
	turtle.turnLeft()
	forward(3)
	down(2)

	logf("INFO", "harvest", "Planting the first deck")
	logRednet("Planting the first deck")
	for i=1, 6 do
		for j=1, 8 do
			turtle.select(2)
			turtle.placeDown()
--			if (i % 2) == 0 then
--				if j < 5 then
--					turtle.placeDown()
--				end
--			else
--				if j >= 5 then
--					turtle.placeDown()
--				end
--			end
			forward(1)
		end
		if (i % 2) == 0 then
			turtle.turnRight()
			forward(1)
			turtle.turnRight()
			forward(1)
		else
			turtle.turnLeft()
			forward(1)
			turtle.turnLeft()
			forward(1)
		end
	end
	
	down(1)

	logf("INFO", "harvest", "Planting the second deck")
	logRednet("Planting the second deck")
	for i=1, 7 do
		for j=1, 8 do
			turtle.select(3)
			turtle.placeDown()
			forward(1)
		end
		if (i % 2) == 0 then
			turtle.turnRight()
			forward(1)
			turtle.turnRight()
			forward(1)
		else
			turtle.turnLeft()
			forward(1)
			turtle.turnLeft()
			forward(1)
		end
	end
	
	down(1)

	logf("INFO", "harvest", "Planting the third deck")
	logRednet("Planting the third deck")
	for i=1, 8 do
		for j=1, 8 do
			turtle.select(4)
			turtle.placeDown()
			forward(1)
		end
		if (i % 2) == 0 then
			turtle.turnLeft()
			forward(1)
			turtle.turnLeft()
			forward(1)
		else
			turtle.turnRight()
			forward(1)
			turtle.turnRight()
			forward(1)
		end
	end
	
	down(1)
	turtle.select(2)
	turtle.drop(64)
	turtle.select(3)
	turtle.drop(64)
	turtle.select(4)
	turtle.drop(64)
	
	up(2)
	forward(8)
	turtle.turnLeft()
	forward(5)
	turtle.turnRight()
	forward(2)
end

commanderId = 39

function waitForCommand()
	logf("INFO", "harvest", "Listening on rednet")
	logRednet("Listening on rednet")
	while true do
		event, param1, param2, param3 = os.pullEvent()
		
		if event == 'key' then
			if param1 == 28 then
				-- Enter key
				return false
			end
		elseif event == 'rednet_message' then
			print("Received '"..param2.."' from "..param1.." at a distance of "..param3)
			if param1 == commanderId and param2 == 'start' then
				return true
			end
		end
	end
end

function logRednet(message)
	rednet.send(commanderId, message)
end

logfOnLog = onLog

statusLoad()
rednet.open("right")
while true do
	if not waitForCommand() then
		break
	end
	print("Harvesting")
	harvest()
end